package 实验八网络编程2;
import java.net.DatagramPacket;  
import java.net.DatagramSocket;  
import java.net.InetAddress;   
public class 八杠三客户端 {  
    private DatagramSocket socket;  
    private byte[] buffer = new byte[1024];  
    private InetAddress serverAddress;  
  
    public 八杠三客户端(String serverHost, int serverPort) throws Exception {  
        socket = new DatagramSocket();  
        serverAddress = InetAddress.getByName(serverHost);  
    }  
  
    public void sendMessage(String message, int serverPort) throws Exception {  
        DatagramPacket packet = new DatagramPacket(message.getBytes(), message.getBytes().length, serverAddress, serverPort);  
        socket.send(packet);  
    }  
  
    public void receiveMessages() throws Exception {  
        while (true) {  
            DatagramPacket packet = new DatagramPacket(buffer, buffer.length);  
            socket.receive(packet);  
            String message = new String(packet.getData(), 0, packet.getLength());  
            System.out.println("Received message: " + message);  
        }  
    }  
}